﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Controls.UserResponse;
using ThreeByte.ActiveDeck.Data.UserResponse;
using ThreeByte.ActiveDeck.Config;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Excel = Microsoft.Office.Interop.Excel;
using ThreeByte.ActiveDeck.AddIn.Config;
using ThreeByte.ActiveDeck.Service;
using System.Xml.Linq;
using System.Drawing;

namespace ThreeByte.ActiveDeck.Controls.UserResponse {
    /// <summary>
    /// Interaction logic for GroupResponseConfiguration.xaml
    /// </summary>
    public partial class GroupedMultipleChoiceResponseConfiguration : UserControl, IUserResponseConfigurationControl {

        private List<MultipleChoiceConfig> mConfigs;
        private List<QuestionConfig> saConfigs;
        private List<CategoriesConfig> catConfigs;
        private CoreConfig cConfig;
        private Dictionary<int, int> pages;

        public GroupedMultipleChoiceResponseConfiguration(List<PageConfig> configs) {
            InitializeComponent();

            cConfig = Globals.ThisAddIn.GetCurrentConfig();
            mConfigs = new List<MultipleChoiceConfig>();
            saConfigs = new List<QuestionConfig>();
            catConfigs = new List<CategoriesConfig>();
            pages = new Dictionary<int, int>();

            foreach(PageConfig pc in configs){

                if(pc is MultipleChoiceConfig) {
                    mConfigs.Add((MultipleChoiceConfig)pc);
				}

                // else if(pc is CategoriesConfig) {
                //    catConfigs.Add((CategoriesConfig)pc);
                //    if(((CategoriesConfig)pc).HasAnswer) {
                //        foreach(Word w in ((CategoriesConfig)pc).Words) {
                //            if(w.
                //        }


                //        for(int i = 0; i < ((CategoriesConfig)pc).Answers.Count; i++) {
                //            if(((CategoriesConfig)pc).Answers[i].IsCorrect) {
                //                pages.Add(pc.PageId, i + 1);
                //            }
                //        }
                //    }
                //} else if(pc is QuestionConfig) {
                //    saConfigs.Add((QuestionConfig)pc);
                //    if(((QuestionConfig)pc).HasAnswer) {
                       
                //            if(((QuestionConfig)pc).Answers[i].IsCorrect) {
                //                pages.Add(pc.PageId, i + 1);
                //            }
                        
                //    }
                //}
                
                                
            }         
        }

        public PowerPoint.Shape InsertUserResponseComponent(PowerPoint.Slide slide) {
             //TODO: Insert centered, at half the height/width of the slide
            PowerPoint.Shape theShape = slide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xl3DBarClustered);
            //Store Section/Page info to the shape
            theShape.Name = @"ActiveDeck_Chart";
            theShape.Tags.Add("Section", mConfigs[0].ParentSlideConfig.SlideId.ToString());
            theShape.Tags.Add("Page", mConfigs[0].PageId.ToString()); //TODO: Get the page business figured out

            List<string> configs = new List<string>();
            foreach(PageConfig pc in mConfigs) {
                configs.Add(pc.ParentSlideConfig.SlideId.ToString() + "," + pc.PageId.ToString());
            }
            theShape.Tags.Add("Pages", String.Join("|", configs));


            theShape.Tags.Add("ConfigType", typeof(MultipleChoiceConfig).ToString()); //TODO: Get the page business figured out
            theShape.Tags.Add("isGroup", "true");


            PowerPoint.Chart theChart = theShape.Chart;
            PowerPoint.ChartData theChartData = theChart.ChartData;

            PowerPoint.Axis myAxis = (PowerPoint.Axis)theChart.Axes(PowerPoint.XlAxisType.xlValue, PowerPoint.XlAxisGroup.xlPrimary);
            myAxis.MaximumScale = 100.0;
            myAxis.MinorUnit = 1.0;            
            myAxis.HasTitle = true;
            myAxis.AxisTitle.Text = "% Correct";

            Excel.Workbook theWorkbook = (Excel.Workbook)(theChartData.Workbook);
            Excel.Worksheet theWorksheet = theWorkbook.Worksheets[1];

            //TODO: Figure out what this actually does
            //gWorkSheet.Range("Table1[[#Headers],[Series 1]]").Value = "Items"
            //Clear out the current sheet data
            theWorksheet.Range["a2", "k20"].Value = null;
            theWorksheet.Range["c1", "k20"].Value = null;
            int i = 1;
            foreach(PresentationGroup pg in cConfig.PresentationGroups) {
                PowerPoint.Series series = (PowerPoint.Series)theChart.SeriesCollection(i);
                PowerPoint.Point point = (PowerPoint.Point)series.Points(1);

                int groupColor = ColorTranslator.ToOle(System.Drawing.Color.FromArgb(pg.Color.A, pg.Color.R, pg.Color.G, pg.Color.B));

                point.Interior.Color = groupColor;
                theWorksheet.Range[string.Format("{0}1", ReportGenerator.GetExcelColumnName(i + 1))].Value = pg.Name;
                theWorksheet.Range[string.Format("{0}2", ReportGenerator.GetExcelColumnName(i + 1))].Value = 0;
                i++;
            }

            //Resize the table
            string extentCell = string.Format("{0}2", ReportGenerator.GetExcelColumnName(i));
            theWorksheet.ListObjects["Table1"].Resize(theWorksheet.Range["A1", extentCell]);

            theChart.Legend.Delete();
            
            //Minimize
            theWorkbook.Application.WindowState = Excel.XlWindowState.xlMinimized;
            return theShape;
        
        }
    }
}
